Skip to main content

Swap Nodes in Pairs

LeetCode 24 | Difficulty: Medium​

Medium

Problem Description​

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

Example 1:

Input: head = [1,2,3,4]

Output: [2,1,4,3]

Explanation:

Example 2:

Input: head = []

Output: []

Example 3:

Input: head = [1]

Output: [1]

Example 4:

Input: head = [1,2,3]

Output: [2,1,3]

Constraints:

- The number of nodes in the list is in the range `[0, 100]`.

- `0 <= Node.val <= 100`

Topics: Linked List, Recursion


Approach​

Linked List​

Use pointer manipulation. Common techniques: dummy head node to simplify edge cases, fast/slow pointers for cycle detection and middle finding, prev/curr/next pattern for reversal.

When to use

In-place list manipulation, cycle detection, merging lists, finding the k-th node.


Solutions​

Solution 1: C# (Best: 92 ms)​

MetricValue
Runtime92 ms
Memory23.1 MB
Date2019-12-15
Solution
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode SwapPairs(ListNode head) {
if(head==null || head.next == null)
{
return head;
}
head.next.next = SwapPairs(head.next.next);
ListNode first = head;
ListNode second = head.next;
first.next = second.next;
second.next = first;
return second;
}
}
πŸ“œ 3 more C# submission(s)

Submission (2022-02-16) β€” 132 ms, 36.9 MB​

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode SwapPairs(ListNode head) {
if(head==null || head.next == null)
{
return head;
}
head.next.next = SwapPairs(head.next.next);
ListNode first = head;
ListNode second = head.next;
first.next = second.next;
second.next = first;
return second;
}
}

Submission (2017-08-07) β€” 156 ms, N/A​

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode SwapPairs(ListNode head) {
if (head == null || head.next == null)
{
return head;
}
ListNode n = head.next;
head.next = SwapPairs(head.next.next);
n.next = head;
return n;
}
}

Submission (2017-08-07) β€” 173 ms, N/A​

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode SwapPairs(ListNode head) {

//iterative
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode current = dummy;
while (current.next != null && current.next.next != null)
{
ListNode first = current.next;
ListNode second = current.next.next;
first.next = second.next;
current.next = second;
current.next.next = first;
current = current.next.next;
}
return dummy.next;
}
}

Complexity Analysis​

ApproachTimeSpace
Linked List$O(n)$$O(1)$

Interview Tips​

Key Points
  • Discuss the brute force approach first, then optimize. Explain your thought process.
  • Draw the pointer changes before coding. A dummy head node simplifies edge cases.